package csse3004.GD.SysAdmin;

/**
 * @author s4201567 Tony Yu
 */
import csse3004.GD.DA.SysAdminDA;
import csse3004.GD.PollAdminAccount;
import java.util.ArrayList;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Tony Yu s4201567
 */
public class ReactivatePAController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private SysAdminService sds = new SysAdminDA();

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        ArrayList<PollAdminAccount> adminList = new ArrayList<PollAdminAccount>();
        adminList = sds.getPollAdminList();
        int deactiveList[] = sds.displayInactiveAdmin();
        
        int i = 0;
        
        if (hsr.getParameter("pid") != null) {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            
            //DB function to reactivate selected poll admin account
            // only display poll admin accounts which have been deactivated
            sds.activateAccount(pid);
            
            String reactive = "The account has successfully been reactivated";
            logger.info(reactive);
            return new ModelAndView("successMsg", "reactive", reactive);
        }
        
        /*
         * TODO CAPTAIN THOMAS.L.MCNAIR BSC, SSC
         * 
         * Only display Poll Administrator accounts which are deactivated
         */
        
        if (i >= 0) {
            ModelAndView mnv = new ModelAndView("reactivatePA", "admin", adminList);
            mnv.addObject("deactive", deactiveList);
            return mnv;
        }
        
        String error = "Page could not load";
        logger.info("Error: " + error);
        
        return new ModelAndView("reactivatePA", "error", error);
    }
}
    
